package com.nineclock.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.document.dto.DocUserCollaborationDTO;
import com.nineclock.document.dto.DocumentUserDTO;
import com.nineclock.document.mapper.DocCollaborationsMapper;
import com.nineclock.document.mapper.DocFileMapper;
import com.nineclock.document.pojo.DocCollaborations;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysCompanyUserFeign;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DocCollaborationsServiceImpl implements DocCollaborationsService {


    @Autowired
    private DocCollaborationsMapper collaborationsMapper;


    @Autowired
    private SysCompanyUserFeign companyUserFeign;


    @Autowired
    private DocFileMapper fileMapper;


    @Override
    public PageResult<DocumentUserDTO> pagingCollaborations(Long id, Integer page, Integer pageSize) {
        // 1.分页查询协作者列表
        // 1.1 创建分页对象，设置分页参数
        IPage<DocCollaborations> iPage= new Page<>(page,pageSize);
        // 1.2 构造查询条件
        LambdaQueryWrapper<DocCollaborations> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId,id);
        // 1.3 执行查询，获取分页查询结果
        iPage = collaborationsMapper.selectPage(iPage, wrapper);
        // 2.封装结果
        // 2.1 获取当前页的数据
        List<DocCollaborations> collaborationsList = iPage.getRecords();
        // 2.2 数据转换，List<DocCollaborations> ---> List<DocumentUserDTO>
        List<DocumentUserDTO> documentUserDTOList=new ArrayList<>();
        if (!CollectionUtils.isEmpty(collaborationsList)){
            documentUserDTOList=collaborationsList.stream().map(docCollaborations -> {
                DocumentUserDTO documentUserDTO = new DocumentUserDTO();
                documentUserDTO.setId(docCollaborations.getId());
                // 根据协作者id调用系统微服务查询协作者信息
                SysCompanyUserDTO companyUserDTO = companyUserFeign.queryCompanyUserById(docCollaborations.getCollaborationId()).getData();
                if (companyUserDTO!=null){
                    documentUserDTO.setUserName(companyUserDTO.getUserName());
                }
                return documentUserDTO;
            }).collect(Collectors.toList());
        }

        return new PageResult<>(iPage.getTotal(),iPage.getPages(),documentUserDTOList);
    }

    @Override
    public List<DocUserCollaborationDTO> queryUsers(Long fileId) {
        if (fileId==null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        List<SysCompanyUserDTO> companyUserDTOList = companyUserFeign.queryAllCompanyUser().getData();

        LambdaQueryWrapper<DocCollaborations> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId,fileId);
        List<DocCollaborations> collaborationsList = collaborationsMapper.selectList(wrapper);
        List<Long> idList = collaborationsList.stream().map(docCollaborations -> {
            return docCollaborations.getCollaborationId();
        }).collect(Collectors.toList());
        DocFile docFile = fileMapper.selectById(fileId);
        List<DocUserCollaborationDTO> collaborationDTOList = companyUserDTOList.stream().map(sysCompanyUserDTO -> {
            DocUserCollaborationDTO collaborationDTO = new DocUserCollaborationDTO();
            collaborationDTO.setId(sysCompanyUserDTO.getId());
            collaborationDTO.setUsername(sysCompanyUserDTO.getUserName());
            collaborationDTO.setImgUrl(sysCompanyUserDTO.getImageUrl());
            collaborationDTO.setPhone(sysCompanyUserDTO.getMobile());
            if (docFile.getCreateUserId() == sysCompanyUserDTO.getId()) {
                collaborationDTO.setState(1);
            } else if (idList.contains(sysCompanyUserDTO.getId())) {
                collaborationDTO.setState(2);
            }
            return collaborationDTO;
        }).collect(Collectors.toList());
        return collaborationDTOList;
    }
}
